{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "23bc3c04",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'str'> <class 'int'>\n",
      "<class 'str'> <class 'int'>\n",
      "<class 'str'> <class 'int'>\n",
      "<class 'str'> <class 'int'>\n",
      "<class 'str'> <class 'int'>\n",
      "<class 'str'> <class 'int'>\n"
     ]
    }
   ],
   "source": [
    "import cv2 as cv\n",
    "import numpy as np\n",
    "from matplotlib import pyplot as plt\n",
    "\n",
    "# 读取模板和待寻找目标的图像\n",
    "imgForSearch = cv.imread('../data/messi5.jpg',0)   #0灰度图\n",
    "imgTemplate = cv.imread('../data/messi_face.jpg',0)\n",
    "# save for drawing matched rect \n",
    "w, h = imgTemplate.shape[::-1]     #元素逆序，start：end：step 若step为负数，则表示逆序\n",
    "\n",
    "# 6种方法\n",
    "methods = ['cv.TM_CCOEFF', 'cv.TM_CCOEFF_NORMED', \n",
    "           'cv.TM_CCORR',  'cv.TM_CCORR_NORMED', \n",
    "           'cv.TM_SQDIFF', 'cv.TM_SQDIFF_NORMED']\n",
    "\n",
    "for meth in methods:\n",
    "    #we need keep org img to be searched every loop so \n",
    "    #we should draw sth on a new img in case that org img is changed\n",
    "    imgForDraw = imgForSearch.copy()\n",
    "    \n",
    "    # 将模板和图像进行匹配得到每个像素的相似度\n",
    "    method = eval(meth)\n",
    "    print(type(meth), type(method))\n",
    "    \n",
    "    # 图像位置的相似度\n",
    "    res = cv.matchTemplate(imgForSearch,imgTemplate,method)\n",
    "    #找到相似度最高的像素和位置并将结果显示\n",
    "    min_val, max_val, min_loc, max_loc = cv.minMaxLoc(res)\n",
    "    # If the method is TM_SQDIFF or TM_SQDIFF_NORMED, take minimum\n",
    "    if method in [cv.TM_SQDIFF, cv.TM_SQDIFF_NORMED]:\n",
    "        top_left = min_loc\n",
    "    else:\n",
    "        top_left = max_loc\n",
    "    \n",
    "    # draw and visualize\n",
    "    bottom_right = (top_left[0] + w, top_left[1] + h)\n",
    "    cv.rectangle(imgForDraw, top_left,bottom_right, 255, 2)\n",
    "    #把数组的所有数据按比例标准化映射到某个范围之间\n",
    "    cv.normalize( res, res, 0, 1, cv.NORM_MINMAX, -1 )\n",
    "\n",
    "\n",
    "#     cv.imshow(\"imgTemplate\", imgTemplate)\n",
    "#     cv.imshow(\"imgOrg\", imgForSearch)\n",
    "    cv.imshow(\"matchRes\", res)\n",
    "    cv.imshow(\"imgForDraw\", imgForDraw)\n",
    "    cv.waitKey(0)\n",
    "\n",
    "cv.destroyAllWindows()\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dc8b6b39",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
